;;;=================================================================
;;;
;;; Subject: PBM to list
;;;
;;;-----------------------------------------------------------------
;;;
;;; Jónathan Heras Vicente
;;; Beginning date: 26-04-10
;;;
;;;=================================================================


(defun str-to-string (str)
  (let ((line (read-line str nil)))
    (if line (concatenate 'string line (str-to-string str)) "")))



(defun string-to-list (string)
  (let ((val (multiple-value-bind (a b) (errorset (subseq string 0 1)) b)))
    (if val 
        (cons (read-from-string val) (string-to-list (subseq string 1)))
      nil)))



(defun split-data (string columns rows)
  (if (equal 0 rows)
      nil
    (cons (string-to-list (subseq string 0 columns))
          (split-data (subseq string columns) columns (1- rows)))))
  
  
  

(defun lolo01-to-loc-aux2 (list i j)
  (cond ((endp list) nil)
        ((equal (car list) 1) (append (list (list i j))
                                      (lolo01-to-loc-aux2 (cdr list) i (1+ j))))
        ((equal (car list) 0) (lolo01-to-loc-aux2 (cdr list) i (1+ j)))))
                                    

(defun lolo01-to-loc-aux1 (list i)
  (cond ((endp list) nil)
        (t (append (lolo01-to-loc-aux2 (car list) i 0)
                   (lolo01-to-loc-aux1 (cdr list) (1+ i))))))


(defun lolo01-to-loc (list)
  (lolo01-to-loc-aux1 list 0)
  )


(defun pbm-to-loc ()
  (let* ((path (CG:ask-user-for-existing-pathname ""))
         (str (open path :direction :input))
         (format (READ-LINE STR NIL))
         (creator (READ-LINE STR NIL))
         (columns-and-rows (READ-LINE STR NIL))
         (rows (read-from-string (subseq columns-and-rows (search " " columns-and-rows))))
         (columns (read-from-string (subseq columns-and-rows 0 (search " " columns-and-rows))))
         (data (str-to-string str)))
    (split-data data columns rows)))

(defun pbm-to-loc-path (path)
  (let* ((str (open path :direction :input))
         (format (READ-LINE STR NIL))
         (creator (READ-LINE STR NIL))
         (columns-and-rows (READ-LINE STR NIL))
         (rows (read-from-string (subseq columns-and-rows (search " " columns-and-rows))))
         (columns (read-from-string (subseq columns-and-rows 0 (search " " columns-and-rows))))
         (data (str-to-string str)))
    (split-data data columns rows)))



(defun file-to-string2 (str)
  (let* ((format (READ-LINE STR NIL))
         (creator (READ-LINE STR NIL))
         (columns-and-rows (READ-LINE STR NIL))
         (rows (read-from-string (subseq columns-and-rows (search " " columns-and-rows))))
         (columns (read-from-string (subseq columns-and-rows 0 (search " " columns-and-rows))))
         (data (str-to-string str)))
    (split-data data columns rows)))



(defun file-to-string-3d (str)
  (let* ((data (str-to-string str)))
    (read-from-string data)))

(defun 3d-image-to-coordinates (str)
  (let* ((data (str-to-string str)))
    (read-from-string data)))







  
  